
export class Pager {
  constructor() {

  }

  go(page){
    this.result.currentPage = page;
    this._calculate();
  }

  next(){
    let currentPage = this.result.currentPage;
    if (++currentPage > this.result.lastPage) {
      return this.result;
    }else {
      this.result.currentPage = currentPage;
      return this._calculate();
    }
  }

  prev(){
    let currentPage = this.result.currentPage;
    if (--currentPage <1) {
      return this.result;
    }else {
      this.result.currentPage = currentPage;
      return this._calculate();
    }
  }

  _triggerMode(){

    const { currentPage, prevGroupPage, nextGroupPage, lastPage, arr }= this.result;
    const triggerRange = this._triggerRange;
    const arrLimit = this._arrLimit;

    if(currentPage === 1)return 0;

    if(prevGroupPage && currentPage === prevGroupPage) return 1;

    if (prevGroupPage) {
      if(arr.slice(0,triggerRange).includes(currentPage)) return 2;
    }

    if (nextGroupPage) {
      if (arr.slice(-triggerRange).includes(currentPage)) return 4;
    }

    if (nextGroupPage && currentPage === nextGroupPage) return 5;

    if (currentPage === lastPage) return 6;

    return 3;
  }

  //生成 prevGroupPage
  _generatePrevGroupPage(arr){
    if (arr[0] === 1) {
      return null;
    }
    let prevGroupPage = arr[0] - 1;

    return prevGroupPage > 1 ?prevGroupPage : 1;
  }

  //生成nextGroupPage
  _generateNextGroupPage(arr){
    if (arr[arr.length - 1] >= this.result.lastPage) {
      return null;
    }
    let nextGroupPage = arr[arr.length - 1]+1;
    return nextGroupPage >= this.result.lastPage ? this.result.lastPage : nextGroupPage;
  }

  //生成 arr
  _generateArr(startPage){
    let arr = [];
    startPage = startPage <= 0 ? 1 : startPage;
    startPage = startPage > this.result.lastPage ? this.result.lastPage : startPage;
    for(let i = 0; i < this._arrLimit; i++){
      let page = startPage + i;
      if (page > this.result.lastPage) break;
      arr.push(page);
    }
    return arr;
  }

  //计算result
  //返回this.result 结果
  _calculate(){

    let totalPageNum,lastPage = this._totalPageNum;
    let triggerRange = this._triggerRange;
    let arrLimit = this._arrLimit;

    let currentPage = this.result.currentPage;
    let arr = this.result.arr;
    let prevGroupPage = null;
    let nextGroupPage = null;

    //第一次初始化
    if (!arr) {

      //生成currentPage
      currentPage =1;

      // 生成arr

      //arr 中渲染出多少页号
      let loopNum = arrLimit < totalPageNum ? arrLimit : totalPageNum;
      arr = this._generateArr(1);

      //生成 nextGroupPage
      //得到下一组起点页号
      nextGroupPage = this._generateNextGroupPage(arr);

      //是否显示lastPage
      lastPage = arr[arr.length - 1] === lastPage ? null : lastPage;

    }else {

      const mode = this._triggerMode();

      let arrStartPage;

      switch (mode) {
        //点击首页
        case 0:
          //生成 arr
          arr = this._generateArr(1);
          prevGroupPage = null;
          nextGroupPage = this._generateNextGroupPage(arr);
          this.dir = "right";
          break;

        //点击了 prevGroupPage
        case 1:
          //生成arr
          //得到arr起始页

          arrStartPage = currentPage - arrLimit +1;
          arrStartPage = arrStartPage > 0 ? arrStartPage : 1;
          arr = this._generateArr(arrStartPage);

          prevGroupPage = this._generatePrevGroupPage(arr);
          nextGroupPage = this._generateNextGroupPage(arr);
          this.dir = "left";
          break;

          //2 点击了arr 中触发向前页
          case 2:
            if (this.result.prevGroupPage) {
              if (this.dir === "right" && currentPage !== arr[0]) {
                return this.result;
              }else {
                arrStartPage = currentPage - this._arrLimit + 1;
                arrStartPage = arrStartPage > 1 ? arrStartPage : 1;
                arr = this._generateArr(arrStartPage);
                prevGroupPage = this._generatePrevGroupPage(arr);
                nextGroupPage = this._generateNextGroupPage(arr);
                this.dir = "left";
              }
            }
            break;

          case 4:
            if (this.result.nextGroupPage) {
              if (this.dir === "left" && currentPage !== arr[arr.length - 1]) {
                return this.result;
              }else {
                arr = this._generateArr(this.result.currentPage);
                prevGroupPage = this._generatePrevGroupPage();
                nextGroupPage = this._generateNextGroupPage();
                this.dir = "right";
              }
            }
            break;

          //5 点击了nextGroupPage
          case 5:
            arrStartPage = currentPage;
            arr = this._ _generateArr(arrStartPage);
            prevGroupPage = this._generatePrevGroupPage(arr);
            nextGroupPage = this._generateNextGroupPage(arr);
            this.dir = "right";
            break;

          // 6 点击了最后一页
          case 6:
          arrStartPage = currentPage - this._arrLimit + 1;
          arrStartPage = arrStartPage > 1 ? arrStartPage : 1;
          arr = this._generateArr(arrStartPage);
          prevGroupPage = this._generatePrevGroupPage(arr);
          this.dir = "left";
          break;

        default:return this.result
      }
    }

    this.result = {
      currentPage,
      arr,
      prevGroupPage,
      nextGroupPage,
      lastPage
    }

    return JSON.parse(JSON.stringify(this.result));
  }


}
